package com.javakc.commonutils.api;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一数据结果返回
 * @author leesiyu
 * @version V1.0
 * @Copyright © 北京汇才同飞教育科技公司
 */

@Data
public class R {
    private Integer code;
    private String message;
    private Boolean success;
    private Map<String, Object> data = new HashMap<>();

    private R() {

    }

    // 返回实体类
    public static R OK() {
        R r = new R();
        r.setCode(ResponseCode.SUCCESS);
        r.setMessage("调用成功!");
        r.setSuccess(true);
        return r;
    }

    public static R ERROR() {
        R r = new R();
        r.setCode(ResponseCode.ERROR);
        r.setMessage("未知的意外错误,请稍后重试!");
        r.setSuccess(false);
        return r;
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    public R success(Boolean success) {
        this.setSuccess(success);
        return this;
    }
    // ??
    public R data(String key, Object value) {
        this.data.put(key, value);     // key是自定义的 任意的
        return this;
    }

    public R data(Map<String, Object> data) {
        this.setData(data);
        return this;
    }
}
